package com.easepal802s.project.ble;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.easepal802s.project.R;

import java.util.List;

public class BluetoothDeviceAdapter extends BaseAdapter {

	Context context;
	List<BtDevice> list;

	public BluetoothDeviceAdapter(Context context, List<BtDevice> list) {
		this.context = context;
		this.list = list;
	}

	public  void  setBtList(List<BtDevice> list){
		this.list=list;
	}

	@Override
	public int getCount() {
		return list==null ? 0 :list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_bluetoothview, parent,false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.device.setText(list.get(position).getName());

		if (list.get(position).isSelect()) {
			viewHolder.device.setBackgroundColor(context.getResources()
					.getColor(R.color.blue));
			viewHolder.device.setTextColor(context.getResources().getColor(
					R.color.white));
		} else {
			viewHolder.device.setBackgroundColor(context.getResources()
					.getColor(R.color.white));
			viewHolder.device.setTextColor(context.getResources().getColor(
					R.color.black));

		}
		viewHolder.device.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				for (int i = 0; i < list.size(); i++) {
					if (i != position) {
						list.get(i).setSelect(false);
					}
				}

				if (!list.get(position).isSelect()) {
					list.get(position).setSelect(true);

				} else {
					list.get(position).setSelect(false);
				}

				BluetoothDeviceAdapter.this.notifyDataSetChanged();
			}
		});
		return convertView;
	}

	class ViewHolder {
		TextView device;

		public ViewHolder(View view) {
			device = (TextView) view.findViewById(R.id.show_every_device);
		}

	}
	}
